package com.itheima.service.impl;

import com.itheima.mapper.LoginMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.LoginInfo;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    //    登录操作
    @Override
    public LoginInfo login(Emp emp) {
        LoginInfo loginInfo = loginMapper.login(emp);

        if(loginInfo != null) {
            LoginInfo info = new LoginInfo(loginInfo.getId(),loginInfo.getUsername(),loginInfo.getName(),null);
            //生成令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",loginInfo.getId());
            claims.put("username",loginInfo.getUsername());
            claims.put("name",loginInfo.getName());
            //生成token
            String token = JwtUtils.generateJwt(claims);
            log.info("生成的令牌是：{}",token);
            info.setToken(token);
            return info;
        }
        return null;
    }
}
